package com.sjd.web.controller.mall;

import com.alibaba.fastjson.JSONObject;
import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.mall.domain.dto.goods.*;
import com.sjd.mall.service.goods.GoodsTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 商品分类管理controller
 * @date : 2022-01-20 22:37
 */
@Slf4j
@RestController
@RequestMapping("mall/goods-type")
public class GoodsTypeController extends BaseController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 查询 管理平台
     *
     * @return return
     */
    @GetMapping()
    @PreAuthorize("@ss.hasPermi('mall:goodsType:list')")
    public TableDataInfo queryTypes(SjdWebSiteGoodsTypeQueryRequestDTO requestDTO) {
        startPage();
        List<SjdWebSiteGoodsTypeQueryResponseDTO> list = goodsTypeService.queryTypesWebSite(requestDTO);
        return getDataTable(list);
    }

    /**
     * 增加商品类型
     *
     * @param responseDTO responseDTO
     * @return return
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('mall:goodsType:add')")
    public AjaxResult addGoodsTypes(@Validated @RequestBody SjdGoodsTypeAddRequestDTO responseDTO) {
        log.info("用户:{} 添加商品类型:{}", getUsername(), JSONObject.toJSONString(responseDTO));
        responseDTO.setCreatorId(getUserId());
        return goodsTypeService.addGoodType(responseDTO);
    }

    /**
     * 修改商品类型
     *
     * @param responseDTO responseDTO
     * @return return
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('mall:goodsType:update')")
    public AjaxResult updateGoodsTypes(@Validated @RequestBody SjdGoodsTypeUpdateRequestDTO responseDTO) {
        log.info("用户:{} 修改商品类型:{}", getUsername(), JSONObject.toJSONString(responseDTO));
        responseDTO.setCreatorId(getUserId());
        return goodsTypeService.updateGoodType(responseDTO);
    }


    /**
     * 删除商品类型
     *
     * @return return
     */
    @PostMapping("/delete/{goodsTypeId}")
    @PreAuthorize("@ss.hasPermi('mall:goodsType:delete')")
    public AjaxResult deleteGoodsTypes(@PathVariable("goodsTypeId") Long goodsTypeId) {
        log.info("用户:{} 修改商品类型:{}", getUsername(), goodsTypeId);
        return goodsTypeService.delGoodsType(goodsTypeId);
    }

}
